<template>
  <div class="app-container">
    <el-row>
      <!-- 顶部搜索栏 -->
      <el-form ref="queryForm" :model="queryParams" v-show="showSearch" :inline="true">
        <el-form-item label="产线" prop="team">
          <el-select v-model="queryParams.areaID" placeholder="请选择产线" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in areaIDOptions" :key="index" :label="item.label" :value="item.value"
                       :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button type="danger" :disabled="multiple" size="mini" @click="popSave">保存</el-button>

        </el-form-item>
      </el-form>

      <!-- 后台回显表格 -->
      <el-table v-loading="loading" :data="pellList" height="700" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="产线" align="center" key="areaID" prop="areaID" v-if="columns[0].visible"  />
        <el-table-column label="产品名称" align="center" prop="defName" width="200" v-if="columns[1].visible" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            <el-input v-model="scope.row.defName"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="定重" align="center" prop="gy_dz" width="100" v-if="columns[2].visible" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-input v-model="scope.row.gy_dz"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="工号" align="center" key="winderName" prop="winderName" v-if="columns[3].visible" :show-overflow-tooltip="true" />
        <el-table-column label="底部描述是否打印" align="center" prop="is_bottom" v-if="columns[4].visible" width="150">
          <template slot-scope="scope">
            <el-select v-model="scope.row.is_bottom" placeholder="请选择" clearable :style="{width: '100%'}">
              <el-option
                v-for="item in bottomOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="定重是否打印" align="center" prop="is_dz" v-if="columns[5].visible" width="150">
          <template slot-scope="scope">
            <el-select v-model="scope.row.is_dz" placeholder="请选择" clearable :style="{width: '100%'}">
              <el-option
                v-for="item in dzOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="顺逆区分" align="center" prop="is_sn" v-if="columns[6].visible" width="150">
          <template slot-scope="scope">
            <el-select v-model="scope.row.is_sn" placeholder="请选择" clearable :style="{width: '100%'}">
              <el-option
                v-for="item in snOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="日期方式" align="center" prop="is_date" v-if="columns[7].visible" width="150">
          <template slot-scope="scope">
            <el-select v-model="scope.row.is_date" placeholder="请选择" clearable :style="{width: '100%'}">
              <el-option
                v-for="item in dateOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"></el-option>
            </el-select>
          </template>
        </el-table-column>
      </el-table>

      <!-- 底部pagination -->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

    </el-row>

    <!-- 用户导入对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col>
            <el-form-item label="班组" prop="team">
              <el-select v-model="form.team" placeholder="请选择班组" clearable :style="{width: '79%'}">
                <el-option v-for="(item, index) in teamOptions" :key="index" :label="item.label" :value="item.value"
                           :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="打印日期">
              <el-date-picker
                v-model="form.printdate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择打印日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listPell ,savePell} from '@/api/wind/save/save'

export default {
  name: "Save",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //络纱标签数据
      pellList: null,
      savePellList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaID: undefined,
      },
      // 列信息
      columns: [
        { key: 0, label: `产线`, visible: true },
        { key: 1, label: `产品名称`, visible: true },
        { key: 2, label: `定重`, visible: true },
        { key: 3, label: `工号`, visible: true },
        { key: 4, label: `底部描述是否打印`, visible: true },
        { key: 5, label: `定重是否打印`, visible: true },
        { key: 6, label: `顺逆区分`, visible: true },
        { key: 7, label: `日期方式`, visible: true  },
      ],
      rules: {
        team: [
          { required: true, message: "班组不能为空", trigger: "blur" }
        ],
        printdate: [
          { required: true, message: "打印日期不能为空", trigger: "blur" }
        ],
      },
      areaIDOptions: [{
        "label": "304",
        "value": "0101304"
      }, {
        "label": "307",
        "value": "0101307"
      }],
      bottomOptions: [{
        "label": "不打印",
        "value": "0"
      }, {
        "label": "打印",
        "value": "1"
      }],
      dzOptions: [{
        "label": "不打印",
        "value": "0"
      }, {
        "label": "打印",
        "value": "1"
      }],
      snOptions: [{
        "label": "不打印",
        "value": "0"
      }, {
        "label": "顺时针",
        "value": "1"
      }, {
        "label": "逆时针",
        "value": "2"
      }],
      dateOptions: [{
        "label": "不打印",
        "value": "0"
      }, {
        "label": "年-月-日",
        "value": "1"
      }, {
        "label": "日-月-年",
        "value": "2"
      }],
      teamOptions: [{
        "label": "甲班",
        "value": "1"
      }, {
        "label": "乙班",
        "value": "2"
      }, {"label": "丙班",
        "value": "3"
      }]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询MES数据列表 */
    getList() {
      this.loading = true;
      listPell(this.queryParams).then(response => {
          this.pellList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 多选框选中数据 **/
    handleSelectionChange(selection) {
      this.savePellList = selection.map(item=> item);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 保存按钮操作 */
    popSave() {
      this.open = true;
      this.title = "保存标签";
    },
    /** 保存 对话框提交按钮 */
    submitFileForm: function() {

      let formData = new FormData();
      formData.append("savePellList", JSON.stringify(this.savePellList));
      formData.append("addParams", JSON.stringify(this.form));
      console.log(formData);
      if (this.form.team == null){
        this.$message({
          message: '警告：请选择班组',
          type: 'warning'
        })
      } else if(this.form.printdate == null) {
        this.$message({
          message: '警告：请选择打印日期',
          type: 'warning'
        })
      } else {
        savePell(formData).then(response => {
          this.msgSuccess("保存成功");
          this.open = false;
        });
      }
    },
    /** 保存对话框取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        team: undefined,
        printdate: undefined
      };
      this.resetForm("form");
    },
  },
}
</script>

<style scoped>

</style>
